import {IMapCreator} from "../imap-creator";
import {MapCreateOption} from "../models/map-create-option";
import {EventHubService} from "../../../services/event-hub.service";
import {GeolocateType} from "../models/geolocate-type";
import {MapApi} from "../../../model/map/MapApi";
import {AdpMapCommon} from '../adp-map-common'

export class BMapCreator implements IMapCreator{

    //地图api key
    apiKey:string = 'M9TZkrLfYBVmMkpiMGoU2NxLA5M9p39X';

    //加载地图api脚本
    loadScript(){
        let script = document.createElement("script");
        script.type = "text/javascript";
        script.src = `http://api.map.baidu.com/api?v=2.0&ak=${this.apiKey}&callback=baiduMapJsLoaded`;
        document.body.appendChild(script);
    }

    private tool:AdpMapCommon;
    constructor(){
        this.tool = new AdpMapCommon();
    }

    //处理地图初始化完成后的定位方式
    getLocation(option: MapCreateOption,mapApi:MapApi,center:any){
        if(option.geolocatedType === GeolocateType.CustomerLocation){   //定位客户所在位置
            EventHubService.emit("map-geolocated",{
                mapApi:mapApi,
                center,
                geolocatedType:GeolocateType.CustomerLocation
            });
        }else if(option.geolocatedType === GeolocateType.UserLocation){    //定位用户所在位置
            this.tool.getUserLatLng({
                mapApi:mapApi,
                geolocatedType:GeolocateType.UserLocation,
                center
            });
        }else if(option.geolocatedType === GeolocateType.DefaultLocation){ //默认行为
            EventHubService.emit("map-geolocated",{
                mapApi:mapApi,
                center,
                geolocatedType:GeolocateType.DefaultLocation
            });
        }else if(option.geolocatedType === GeolocateType.NoneLocation){  //无需定位

        }else{                                                              //缺省值
            EventHubService.emit("map-geolocated",{
                mapApi:mapApi,
                center,
                geolocatedType:GeolocateType.DefaultLocation
            });
        }
    }

    //创建地图
    createMap(option: MapCreateOption) {
        let self = this;
        //加载地图脚本
        let jsLoaded = window['baiduMapJsLoaded'] = window['baiduMapJsLoaded'] || function(ops = undefined ){
            let createMapOptions = ops || option;
            let bmapApi = window["BMap"];
            let map = new bmapApi.Map(createMapOptions.wrapperId);          // 创建地图实例
            let point = new bmapApi.Point(createMapOptions.lng,createMapOptions.lat);  // 创建点坐标
            map.centerAndZoom(point, 14);                 // 初始化地图，设置中心点坐标和地图级别
            map.enableScrollWheelZoom();

            let mapApi = new MapApi();
            mapApi._mapApi = bmapApi;
            mapApi._mapInstance = map;
            EventHubService.emit("map-ready",{
                mapApi:mapApi,
                center:point
            });
            //根据定位类型定位地图位置
            self.getLocation(createMapOptions,mapApi,point);
        };

        //region 判断地图脚本是否加载
        let mapApi = window['BMap'];
        if(mapApi){
            jsLoaded(option);
        }else{
            this.loadScript();
        }
        //endregion


    }

}
